<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * hir_cimke CRUD model class 
 *
 * @author Gabor Bota <gab.bota at 3pixel.hu>
 */
Class Hir_cimke_model extends CI_Model {

    public $hir_table;
    public $relationship_table;
    public $hir_cimke_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        // dbprefixek
        $this->hir_table = $this->db->dbprefix('hirek');
        $this->relationship_table = $this->db->dbprefix('term_taxonomy_relationships');
        $this->hir_cimke_table = $this->db->dbprefix('hir_cimke');
    }

    /**
     * 
     * 
     * 
     * Update hir_cimke by id.
     * 
     * 
     * @return true on success
     */
    function update_hir_cimke($hir_cimke_data = array()) {
        // if hir_cimke does not exists throw hir_cimke_not_exists_exception
        if ($this->get_hir_cimke($hir_cimke_data['cimke_id']) == FALSE)
            throw new not_exists_exception("hir_cimke");

        // do selection and update data
        $this->db->where('cimke_id', $hir_cimke_data['cimke_id']);
        $this->db->update($this->hir_cimke_table, $hir_cimke_data);
    }

    /**
     * 
     * 
     * 
     * Delete hir_cimke
     * 
     * 
     * 
     * @param type $id
     */
    function delete($cimke_id) {
        // kitörlöm az összes ilyen id-jű kapcsolatot a kapcsolatok táblából
        $this->db->where('taxonomy_table_name', $this->hir_cimke_table);
        $this->db->where('taxonomy_id', $cimke_id);
        $this->db->delete($this->relationship_table);
        // delete hir_cimke data from hir_cimke_table
        $this->db->where('cimke_id', $cimke_id);
        $this->db->delete($this->hir_cimke_table);
    }

    /**
     * 
     * Add new hir_cimke
     * 
     * @param type $hir_cimke_data
     */
    function add_hir_cimke($hir_cimke_data = array()) {
        // insert hir_cimke_data
        $this->db->insert($this->hir_cimke_table, $hir_cimke_data);
    }

    /**
     * 
     * returns the hir_cimke (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned hir_cimke
     * @param type $offset from this number.
     * @return type 
     */
    function get_hir_cimkek($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (isset($options['search_like'])) {
            $this->db->like('hir_cimke_nev', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->hir_cimke_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of hir_cimke
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->hir_cimke_table);
    }

    /**
     * Gives back hir_cimke by id 
     * 
     * @param type $id
     * @return array (cimke_id, hir_cimkename, hir_cimke)
     */
    function get_hir_cimke($id) {
        // constructing active record query
        $query = $this->db->get_where($this->hir_cimke_table, array('cimke_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * Gives back hir_cimke by id 
     * 
     * @param type $id
     * @return array (cimke_id, hir_cimkename, hir_cimke)
     */
    function get_hir_cimke_by_slug($slug) {
        // constructing active record query
        $query = $this->db->get_where($this->hir_cimke_table, array('cimke_nev_slug' => $slug));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }
    
    /**
     * Gives back hir_cimke by id 
     * 
     * @param type $id
     * @return array (cimke_id, hir_cimkename, hir_cimke)
     */
    function get_hir_cimke_by_nev($nev) {
        // constructing active record query
        $query = $this->db->get_where($this->hir_cimke_table, array('cimke_nev' => $nev));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    } 

    /**
     * 
     * a $starting_chars karakterekkel kezdődő 
     * cimkék neveit adja vissza
     * 
     */
    function get_hir_cimke_by_start($starting_chars) {
        // select with regexp
        $this->db->where('cimke_nev REGEXP', '^' . $starting_chars);
        $query = $this->db->get($this->hir_cimke_table);
        // do the query
        $result = $query->result();
        return $result;
    }

    /**
     * 
     * cimkéket ment,
     * jöhet array-ban vagy egyenként.
     * ha már létezik (slug-ra ellenőriz) akkor nem ment
     * simán visszatér
     * 
     */
    function save_hir_cimke($cimkek = array()) {
        foreach ($cimkek as $cimke) {
            // ha még nincs ilyen cimke
            if (is_null($this->get_hir_cimke_by_nev($cimke))) {
                // hozzáadjuk
                $hir_cimke_data = array(
                    'cimke_nev' => $cimke,
                    'cimke_nev_slug' => _generate_slug($cimke)
                );
                $this->add_hir_cimke($hir_cimke_data);
            }
        }
    }

    /**
     * 
     * beteszi a hir címkéket a hírhez.
     * slug-ot is, rendes nevet is.
     * 
     */
    function hir_cimkek_hirhez(&$hir) {
        // megszerezzük a kapcsolatokat
        $this->db->where('term_table_name', $this->hir_table);
        $this->db->where('term_id', $hir->hir_id);
        $this->db->where('taxonomy_table_name', $this->hir_cimke_table);
        $query = $this->db->get($this->relationship_table);
        $relation_result = $query->result();
        if ($relation_result) {
            // egyenként megszerezzük a labelek nevét
            foreach ($relation_result as $relation) {
                $this->db->where('cimke_id', $relation->taxonomy_id);
                $query = $this->db->get($this->hir_cimke_table);
                $cimke_result = $query->result();
                // hozzáadjuk, | separator! a végére is kell!
                $hir->hir_cimkek .= $cimke_result[0]->cimke_nev . "|";
                $hir->hir_cimke_slugok .= $cimke_result[0]->cimke_nev_slug . "|";
            }
        }
    }

}